package com.ruoyi.web.controller.base;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseSupplier;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseSupplierService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商信息Controller
 *
 * @author ruoyi
 * @date 2023-02-01
 */
@Api(tags = "供应商信息api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/supplier" )
public class BaseSupplierController extends BaseController {

    private final IBaseSupplierService iBaseSupplierService;

    /**
     * 查询供应商信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseSupplier baseSupplier)
    {
        startPage();
        LambdaQueryWrapper<BaseSupplier> lqw = new LambdaQueryWrapper<BaseSupplier>();
        if (StringUtils.isNotBlank(baseSupplier.getSupplierName())){
            lqw.like(BaseSupplier::getSupplierName ,baseSupplier.getSupplierName());
        }
        if (StringUtils.isNotBlank(baseSupplier.getSupplierCode())){
            lqw.eq(BaseSupplier::getSupplierCode ,baseSupplier.getSupplierCode());
        }
        List<BaseSupplier> list = iBaseSupplierService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 查询供应商信息列表获取所有
     */
    @ApiOperation(value = "查询供应商信息列表获取所有")
    @GetMapping("/getList")
    public List<BaseSupplier> getList(BaseSupplier baseSupplier)
    {
        LambdaQueryWrapper<BaseSupplier> lqw = new LambdaQueryWrapper<BaseSupplier>();
        if (StringUtils.isNotBlank(baseSupplier.getSupplierName())){
            lqw.like(BaseSupplier::getSupplierName ,baseSupplier.getSupplierName());
        }
        if (StringUtils.isNotBlank(baseSupplier.getSupplierCode())){
            lqw.eq(BaseSupplier::getSupplierCode ,baseSupplier.getSupplierCode());
        }
        List<BaseSupplier> list = iBaseSupplierService.list(lqw);
        return list;
    }

    /**
     * 导出供应商信息列表
     */
    @Log(title = "供应商信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseSupplier baseSupplier) {
        LambdaQueryWrapper<BaseSupplier> lqw = new LambdaQueryWrapper<BaseSupplier>(baseSupplier);
        List<BaseSupplier> list = iBaseSupplierService.list(lqw);
        ExcelUtil<BaseSupplier> util = new ExcelUtil<BaseSupplier>(BaseSupplier. class);
        return util.exportExcel(list, "supplier" );
    }

    /**
     * 获取供应商信息详细信息
     */
    @GetMapping(value = "/{supplierId}" )
    public AjaxResult getInfo(@PathVariable("supplierId" ) Long supplierId) {
        return AjaxResult.success(iBaseSupplierService.getById(supplierId));
    }

    /**
     * 新增供应商信息
     */
    @Log(title = "供应商信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseSupplier baseSupplier) {
        baseSupplier.setCreateBy(SecurityUtils.getUsername());
        baseSupplier.setCreateTime(DateUtil.parse(DateUtil.now()));
        return toAjax(iBaseSupplierService.save(baseSupplier) ? 1 : 0);
    }

    /**
     * 修改供应商信息
     */
    @Log(title = "供应商信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseSupplier baseSupplier) {
        return toAjax(iBaseSupplierService.updateById(baseSupplier) ? 1 : 0);
    }

    /**
     * 删除供应商信息
     */
    @Log(title = "供应商信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierIds}" )
    public AjaxResult remove(@PathVariable Long[] supplierIds) {
        return toAjax(iBaseSupplierService.removeByIds(Arrays.asList(supplierIds)) ? 1 : 0);
    }
}
